<?php
namespace app\shop\controller;
use app\ApiRest;
use app\Rest;
use app\shop\model\IndexDistributor;
use think\App;
use think\Request;
use app\shop\model\IndexUser as UserModel;
use app\shop\model\IndexAddress;


class IndexUser extends ApiRest
{

    protected $model;
    protected $distrbutor_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model         = new UserModel();
        $this->address_model = new IndexAddress();
        $this->distrbutor_model = new IndexDistributor();
    }
    /**
     * 获取个人信息
     */
    public function userInfo(){
        $input = $this->_input;
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        $dis[] = ['a.id' ,'=',$this->getUserId()];
        $data  = $this->model->userInfo($dis);
        return $this->success($data);
    }

    /**
     *
     * 获取地址列表
     */

    public function addressList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['user_id','=',$this->getUserId()];

        $dis[] = ['status' ,'=',1];

        if(!empty($input['is_city'])){

            $dis[] = ['long' ,'<>',0];

        }
        $data  = $this->address_model->addressList($dis);

        return $this->success($data);
    }

    /**
     * 添加地址
     */
    public function addressAdd(){
        $input = $this->_input;
        $dis[] = ['user_id','=',$this->getUserId()];
        $dis[] = ['status','=',1];
        $data  = $this->address_model->addressInfo($dis);
        $input['is_default'] = !empty($data)?0:1;
        $input['user_id']    = $this->getUserId();
        $input['uniacid']    = $this->_uniacid;
        $res   = $this->address_model->addressAdd($input);
        return $this->success($res);
    }

    /**
     * 编辑地址
     */
    public function addressUpdate(){
        $input = $this->_input;
        $dis[] = ['user_id','=',$this->getUserId()];
        if(count($input)>1){
            $dis[] = ['id','=',$input['id']];
            if(!empty($input['is_default'])&&$input['is_default']==1){

                $diss[] = ['uniacid','=',$this->_uniacid];

                $diss[] = ['user_id','=',$this->getUserId()];

                $diss[] = ['status' ,'=',1];

                $diss[] = ['is_default' ,'=',1];

                $res    = $this->address_model->addressUpdate($diss,['is_default'=>0]);
            }
            $res = $this->address_model->addressUpdate($dis,$input);
        }else{
            $res = $this->address_model->addressInfo($dis);
        }
        return $this->success($res);

    }

    /**
     *
     * 默认地址
     */

    public function addressDefault(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['user_id','=',$this->getUserId()];

        $dis[] = ['status' ,'=',1];

        $dis[] = ['is_default' ,'=',1];

        if(!empty($input['is_city'])){

            $dis[] = ['long' ,'<>',0];

        }
        $data  = $this->address_model->addressInfo($dis);
        return $this->success($data);

    }


    /**
     * @author 郑文军
     * @DataTime: 2020/5/19 9:29
     * @功能说明:申请分销员
     */
    public function applicationDistributor()
    {
        $input = $this->_input;
        $userinfo = $this->getUserInfo();
        $where['user_id'] = $userinfo['id'];
        $where ['uniacid'] = $userinfo['uniacid'];
        $data = $this->distrbutor_model->addDistributor($where, $input);
        if($data['code']==0){
            return $this->error($data['msg']);
        }
        return $this->success([]);
    }

    /**
     * @author 郑文军
     * @DataTime: 2020/5/21 13:58
     * @功能说明:获取分销员当前状态
     */
    public function getDistributorStatus()
    {
        $userinfo = $this->getUserInfo();
        $where['user_id'] = $userinfo['id'];
        $where ['uniacid'] = $userinfo['uniacid'];
        $data = $this->distrbutor_model->getDistributorStatus($where);
        return $this->success(['status' => $data]);
    }



}
